package com.Sake.web.controller.Home;

import com.Sake.common.annotation.LoggerManage;
import com.Sake.system.service.HomeImageService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 主页图片控制器
 *
 * @author zsh
 * @date 2021.7.1
 */
@RestController
@RequestMapping("/home/image")
public class HomeImageController {

    /**
     * 主页图片Service
     */
    @Autowired
    private HomeImageService homeImageService;

    @GetMapping(value = "/getAllImages", produces = "application/json", consumes = "application/json")
    @LoggerManage(moduleName = "主页图片设置", option = "获取所有图片列表")
    public JSONObject getAllImages() {
        try {
            return homeImageService.getAllImages();
        } catch (Exception e) {
            throw new RuntimeException("主页图片设置  -  获取所有图片列表  -  异常", e);
        }
    }

    @PostMapping(value = "/editImage", produces = "application/json", consumes = "multipart/form-data")
    @LoggerManage(moduleName = "主页图片设置", option = "单个图片修改")
    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    public JSONObject editImage(@RequestParam MultipartFile file, Integer data) {
        try {
            // data : 修改图片的标记 1主页背景 2主页头像 3登录页背景 4登录页头像
            return homeImageService.editImage(file, data);
        } catch (Exception e) {
            throw new RuntimeException("主页图片设置  -  单个图片修改  -  异常", e);
        }
    }

    @PostMapping(value = "/editImages", produces = "application/json", consumes = "multipart/form-data")
    @LoggerManage(moduleName = "主页图片设置", option = "多图修改")
    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    public JSONObject editImages(@RequestParam(value = "backgroundImage", required = false) MultipartFile backgroundImage,
                                 @RequestParam(value = "headPortrait", required = false) MultipartFile headPortrait,
                                 @RequestParam(value = "backgroundImage_journal", required = false) MultipartFile backgroundImage_journal,
                                 @RequestParam(value = "headPortrait_journal", required = false) MultipartFile headPortrait_journal,
                                 @RequestParam(value = "backgroundImage_photoAlbum", required = false) MultipartFile backgroundImage_photoAlbum,
                                 @RequestParam(value = "headPortrait_photoAlbum", required = false) MultipartFile headPortrait_photoAlbum,
                                 @RequestParam(value = "backgroundImage_personalFile", required = false) MultipartFile backgroundImage_personalFile,
                                 @RequestParam(value = "headPortrait_personalFile", required = false) MultipartFile headPortrait_personalFile,
                                 @RequestParam(value = "backgroundImage_share", required = false) MultipartFile backgroundImage_share,
                                 @RequestParam(value = "headPortrait_share", required = false) MultipartFile headPortrait_share,
                                 @RequestParam(value = "loginPageBackground", required = false) MultipartFile loginPageBackground,
                                 @RequestParam(value = "loginPageAvatar", required = false) MultipartFile loginPageAvatar) {
        try {
            // 主页背景 主页头像 登录页背景 登录页头像
            return homeImageService.editImages(backgroundImage, headPortrait,
                    backgroundImage_journal, headPortrait_journal,
                    backgroundImage_photoAlbum, headPortrait_photoAlbum,
                    backgroundImage_personalFile, headPortrait_personalFile,
                    backgroundImage_share, headPortrait_share,
                    loginPageBackground, loginPageAvatar);
        } catch (Exception e) {
            throw new RuntimeException("主页图片设置  -  多图修改  -  异常", e);
        }
    }

}
